<!-- 预警跟踪 -->
<script>
import { findListByPage, getStatus } from '@/api/handle'
import { followUpSubmit } from './submit'
import QueryTable from '@/mixins/QueryTable'
import { NextgetDistrictTree } from '@/api/system'
import { mapGetters } from 'vuex'

export default {
  name: 'indexTable',
  extends: QueryTable,
  props: {
    active: {
      type: String,
      default: ''
    }
  },
  data() {
    const areaCode = this.$store.state.user.user.districtNo
    return {
      inputList: {
        data: [
          {
            label: '对象所属区域',
            type: 'cascader',
            list: [],
            // children: 'childNode',
            // value: 'districtNo',
            // text: 'text',
            // showCount: true,
            col: 8,
            prop: 'areaCode',
            showAllLevels: false,
            props: {
              expandTrigger: 'hover',
              checkStrictly: true,
              value: 'districtNo',
              label: 'text',
              children: 'childNode'
            },
            change: (val) => {
              if (val) {
                // this.getItemByCategoryId(val[val.length - 1])
              }
            }
          },
          // {
          //   type: 'select',
          //   label: '对象所属区域',
          //   prop: 'areaCode',
          //   list: []
          // },
          {
            type: 'select',
            label: '跟进状态',
            prop: 'status',
            list: []
          }
        ]
      },
      tableApi: findListByPage, // 表格数据API
      tableConfig: {
        columns: [
          {
            label: '预警编号',
            prop: 'warningCode',
            showTooltip: true
          },
          {
            label: '预警详情',
            prop: 'detailContent',
            showTooltip: true
          },
          {
            label: '模型类型',
            prop: 'modelTypeName',
            showTooltip: true
          },
          {
            label: '预警对象所属区域',
            prop: 'fullPath',
            showTooltip: true,
            width: 200
          },
          {
            label: '预警时间',
            prop: 'warningTime',
            showTooltip: true
          },
          {
            label: '跟进状态',
            prop: 'statusName',
            showTooltip: true,
            width: 100
          }
        ],
        btns: [
          {
            text: '跟进',
            methods: (row) => this.handleView(row, 'follow')
          },
          {
            text: '关闭预警',
            hide: (row) => row.status != '0',
            methods: (row) => this.targetStatus(row, '关闭预警')
          },
          {
            text: '解除预警',
            hide: (row) => row.status != '1',
            methods: (row) => this.targetStatus(row, '解除预警')
          },
          {
            text: '查看',
            methods: (row) => this.handleView(row, 'look')
          }
        ]
      },
      customQueryParam: {
        modelType: localStorage.modelType || '1',
        type: this.$route.path == '/warning/query' ? 1 : 2
      },
      isQuery: false,
      areaCode: ''
    }
  },
  computed: {
    ...mapGetters(['user'])
  },
  created() {
    this.areaCode = this.$store.state.user.user.districtNo
    this.isQuery = this.$route.path == '/warning/query'
    this.getDict()
    this.setBtns()
    this.setLabels()
  },

  mounted() {},
  watch: {
    active(val) {
      this.customQueryParam.modelType = val
      this.inputList.model.areaCode = [this.areaCode]
      this.inputList.model.status = ''
      this.handleSearch()
      this.setLabels(val)
    }
  },

  methods: {
    setBtns() {
      let flag = this.$route.path == '/warning/query'
      if (!flag) {
        this.tableConfig.btns = [
          {
            text: '跟进',
            methods: (row) => this.handleView(row, 'follow')
          },
          {
            text: '关闭预警',
            hide: (row) => row.status != '0',
            methods: (row) => this.targetStatus(row, '关闭预警')
          },
          {
            text: '解除预警',
            hide: (row) => row.status != '1',
            methods: (row) => this.targetStatus(row, '解除预警')
          },
          {
            text: '查看',
            methods: (row) => this.handleView(row, 'look')
          }
        ]
      } else {
        this.tableConfig.btns = [
          {
            text: '查看',
            methods: (row) => this.handleView(row, 'look')
          }
        ]
      }
    },
    setLabels(val) {
      let flag = this.$route.path == '/warning/query'
      let label = ''
      if (val) {
        label = val == 5 ? '预警接收对象所属区域' : '对象所属区域'
        this.inputList.data[0].label = label
        this.tableConfig.columns[3].label = label
      }
      if (flag) {
        this.inputList.data[1].label = '状态'
      }
    },
    getDict() {
      // this.inputList.data[0].list = [
      //   { label: '本区域', value: 1 },
      //   { label: '下级区域', value: 2 }
      // ]
      // this.inputList.data[1].list = [
      //   { label: '全部', value: '' },
      //   { label: '待激活', value: 1 },
      //   { label: '待激活', value: 2 }
      // ]
      this.queryAreaTree()
      this.getStatus()
    },
    getDefaultModel() {
      let areaCode = this.$store.state.user.user.districtNo
      if (this.areaCode instanceof Array) {
        areaCode = this.areaCode.at(-1)
      }
      return {
        areaCode,
        status: '',
        modelType: '1'
      }
    },
    handleView(row, type) {
      this.$router.push({
        path: '/warning/track/detail',
        query: {
          type,
          modelType: this.active,
          warningCode: row.warningCode
        }
      })
    },
    targetStatus(row, type) {
      this.row = row
      switch (type) {
        case '跟进':
          handleView(row, 'follow')
          break
        case '关闭预警':
          followUpSubmit.call(this, 1)
          break
        case '解除预警':
          followUpSubmit.call(this, 2)
          break
      }
    },
    // 区域查询
    async queryAreaTree() {
      const { districtId, districtName, districtNo, poltype } = this.user
      const { data } = await NextgetDistrictTree({ pid: districtId })
      const tree = [
        {
          districtNo: districtNo,
          id: districtId,
          // parentId: '1',
          poltype: poltype,
          text: districtName,
          childNode: data
        }
      ]
      this.inputList.data[0].list = tree
    },
    // 状态
    getStatus() {
      getStatus({}).then((res) => {
        console.log('res: ', res)
        if (!res.success) return
        let list = []
        res.data.map((i) => {
          list.push({ label: i.codeName, value: i.codeValue })
        })
        this.inputList.data[1].list = list
      })
    }
  }
}
</script>
